export function geek(canvasid,data){
  let nav = wx.createCanvasContext(canvasid);
  //width:120;height:460;
  let width = 0;
  wx.getSystemInfo({
    success: function (res) { width = res.screenWidth }
  })


  for(let a=0;a<4;a++){
    if(!data[a]){
      data[a]=0;
    }
  }
console.log(data)
  let SCREEN = width / 750;
  let w0 = 400 * 4.6 * SCREEN;
  let w1 = data[0] ? 17 * 4.6 * SCREEN : w0;
  let w2 = data[1] ? (data[3] + 17) * 4.6 * SCREEN : w0;
  let w3 = data[2] ? (data[2] + data[3] + 17) * 4.6 * SCREEN : w0;
  let w4 =  (data[2] + data[3] + data[1] + 17) * 4.6 * SCREEN;
  
  let LIN = 100 * 4.6 * SCREEN;

  let Style = [w1, w2,w3,w4];
 
  
  let Color = [["#FAD4B5", "#FAD4B5"], ["#F5B59B", "#EC7F91"], ["#EB7A8F", "#DF2263"], ["#CA165A", "#620025"]]

  for (let a = 0; a < 4; a++) {
    let one = nav.createLinearGradient(0, Style[a], 0, LIN);
    let le = 10-Math.floor(Math.random() * 20);
    let ri = 10-Math.floor(Math.random() * 20);
    let t = Color[a][0];
    let v = Color[a][1];
    one.addColorStop(0, t);
    one.addColorStop(1, v);

    nav.beginPath();
    nav.setFillStyle(one)
    // nav.moveTo(0, Style[a]);
    // nav.bezierCurveTo(0+ri, Style[a] - le, SCREEN * 120-le, Style[a] + ri, SCREEN * 120, Style[a])
    // nav.lineTo(SCREEN * 120, LIN);
    // nav.lineTo(0, LIN);
    nav.moveTo(0,LIN);
    nav.lineTo(SCREEN * 120, LIN);
    nav.lineTo(SCREEN * 120, Style[a])
    nav.bezierCurveTo(SCREEN * 120 - le, Style[a] + ri, 0 + ri, Style[a] - le,0,Style[a])
    nav.closePath();
    nav.fill();
  }
  return nav

  
}

export function radar(drink){
  let rect = {};
  wx.getSystemInfo({
    success: function (res) {
      rect.width = res.screenWidth
    },
  });

  const cro = rect.width / 2;
  const zero = rect.width / 750 * 137;
  const ten = (cro - zero) / 4;
  const wlx = Math.sqrt(Math.pow(cro - zero, 2) * 2) / 2;
  const r = rect.width / 750 * 4;//大点r
  const jt = rect.width / 750 * 2;//小点r
  const li = rect.width / 750 * 1;//线条粗细
  const fw = rect.width / 750 * 3;
  const wa = rect.width / 750 * 4;
  const fo = rect.width / 750 * 24;//字体
  const sp = rect.width / 750 * 50;//边字距
  let dian = {
    d1: 80,
    d2: 50,
    d3: 70,
    d4: 80,
    d5: 70,
    d6: 70,
    d7: 90,
    d8: 80
  }//数据，从21点方向顺时钟
 
  let ary = [8,1,3,5,6,2,7,4];
  let eight= [],eitem=[];
  
 for (let i in drink){
   eight.push(drink[i])
 };

  eight.map((evl,index)=>{
    eitem.push(eight[ary[index]-1]*10) 
  })

  for (let a=1; a < 9 ; a++){
      dian['d'+a]=eitem[a-1]
  };
  
  function ban(f) {
    return Math.sqrt(Math.pow(f, 2) * 2) / 2
  }




  let da = {
    d1: (function () {
      let l = (cro - zero) * dian.d1 / 100;
      let oi = ban(l);
      return {
        x: cro - oi,
        y: cro - oi
      }
    })(),
    d2: (function () {
      let l = (cro - zero) * dian.d2 / 100;
      return {
        x: cro,
        y: cro - l
      }
    })(),
    d3: (function () {
      let l = (cro - zero) * dian.d3 / 100;
      let oi = ban(l);
      return {
        x: cro + oi,
        y: cro - oi
      }
    })(),
    d4: (function () {
      let l = (cro - zero) * dian.d4 / 100;
      return {
        x: cro + l,
        y: cro
      }
    })(),
    d5: (function () {
      let l = (cro - zero) * dian.d5 / 100;
      let oi = ban(l);
      return {
        x: cro + oi,
        y: cro + oi
      }
    })(),
    d6: (function () {
      let l = (cro - zero) * dian.d6 / 100;
      return {
        x: cro,
        y: cro + l
      }
    })(),
    d7: (function () {
      let l = (cro - zero) * dian.d7 / 100;
      let oi = ban(l);
      return {
        x: cro - oi,
        y: cro + oi
      }
    })(),
    d8: (function () {
      let l = (cro - zero) * dian.d8 / 100;
      return {
        x: cro - l,
        y: cro
      }
    })()
  }

  var nav = wx.createCanvasContext("canvas_radar");
  nav.setLineWidth(li);
  nav.setLineCap("round");
  nav.setStrokeStyle("#999999");
  nav.beginPath();
  nav.arc(cro, cro, cro - zero, 0, Math.PI * 2, false);


  nav.arc(cro, cro, cro - ten - zero, 0, Math.PI * 2, false);


  nav.arc(cro, cro, cro - ten * 2 - zero, 0, Math.PI * 2, false);


  nav.arc(cro, cro, cro - ten * 3 - zero, 0, Math.PI * 2, false);


  nav.stroke();



  nav.beginPath();

  nav.moveTo(cro, zero);
  nav.lineTo(cro, cro * 2 - zero);
  nav.stroke();


  nav.beginPath();

  nav.moveTo(zero, cro);
  nav.lineTo(cro * 2 - zero, cro);
  nav.stroke();

  let x1 = cro - wlx,
    y1 = cro + wlx;

  nav.beginPath();

  nav.moveTo(x1, x1);
  nav.lineTo(y1, y1)
  nav.stroke();

  nav.beginPath();

  nav.moveTo(y1, x1);
  nav.lineTo(x1, y1);
  nav.stroke();




  let pi = Math.PI * 2;

  nav.beginPath();
  nav.setFillStyle("#771650");
  nav.setLineWidth(0);
  nav.arc(cro, zero, r, 0, pi, false);
  nav.fill();


  nav.beginPath();
  nav.arc(x1, x1, r, 0, pi, false);
  nav.fill()

  nav.beginPath();
  nav.arc(y1, x1, r, 0, pi, false);
  nav.fill();

  nav.beginPath();
  nav.arc(y1, y1, r, 0, pi, false);
  nav.fill();

  nav.beginPath();
  nav.arc(x1, y1, r, 0, pi, false);
  nav.fill();

  nav.beginPath();
  nav.arc(cro * 2 - zero, cro, r, 0, pi, false);
  nav.fill();

  nav.beginPath();
  nav.arc(zero, cro, r, 0, pi, false);
  nav.fill();

  nav.beginPath();
  nav.arc(cro, cro * 2 - zero, r, 0, pi, false);
  nav.fill();

  nav.beginPath();
  nav.arc(cro, cro, r, 0, pi, false);
  nav.fill();


  nav.beginPath();
  nav.arc(cro, cro - ten * 2, jt, 0, pi, false);
  nav.fill();


  nav.beginPath();
  nav.arc(cro, cro - ten * 3, jt, 0, pi, false);
  nav.fill();


  nav.beginPath();
  nav.arc(cro, cro - ten, jt, 0, pi, false);
  nav.fill();



  nav.beginPath();
  nav.setFontSize(fo);
  nav.setFillStyle("#000000");
  nav.setTextAlign("center");
  nav.setTextBaseline("middle");
  nav.fillText('复杂度', x1 - sp, x1);


  nav.fillText('单宁', y1 + sp, x1);

  nav.fillText('口味度', cro * 2 - zero + sp, cro);

  nav.fillText('酒体', y1 + sp, y1);


  nav.fillText('浓郁度', x1 - sp, y1);

  nav.fillText('酒精度', zero - sp, cro);


  nav.setTextBaseline("bottom");
  nav.fillText('甜度', cro, cro * 2 - zero + sp);

  nav.setTextBaseline("top");
  nav.fillText('酸度', cro, zero - sp);

  // nav.fillText('25', cro, cro - ten);
  // nav.fillText('50', cro, cro - ten * 2);
  // nav.fillText('75', cro, cro - ten * 3);
  // nav.fillText('100', cro, cro - ten * 4)


  const grd = nav.createLinearGradient(0, cro * 2, cro * 2, 0);
  grd.addColorStop(0, "#BD1241");
  grd.addColorStop(1, "#771650");
  nav.beginPath()
  nav.setFillStyle("rgba(0,0,0,0.1)");
  nav.setStrokeStyle(grd);
  nav.setLineWidth(wa);
  nav.moveTo(da.d1.x, da.d1.y);
  nav.lineTo(da.d2.x, da.d2.y);
  nav.lineTo(da.d3.x, da.d3.y);
  nav.lineTo(da.d4.x, da.d4.y);
  nav.lineTo(da.d5.x, da.d5.y);
  nav.lineTo(da.d6.x, da.d6.y);
  nav.lineTo(da.d7.x, da.d7.y);
  nav.lineTo(da.d8.x, da.d8.y);
  nav.lineTo(da.d1.x, da.d1.y);
  nav.stroke();
  nav.fill();

  nav.closePath();

  let result=new Promise(function(resolve,reject){
    nav.draw(false, function () {
      setTimeout(function(){
        wx.canvasToTempFilePath({
          canvasId: 'canvas_radar',
          success: function (res) {
            resolve(res)
          }
        }, this)
      },300)
    });

  } )
 return result
}

export function rank(canvasid,data){
  let width=''
  wx.getSystemInfo({
    success: function(res) {
    width=res.screenWidth
    },
  });
  let o=465*width/750/2;
  let lin = 30 * width/750;//线
  let cao=Number(data)/50+1.5;
  let nav=wx.createCanvasContext(canvasid, this)
  console.log(o)
  nav.setLineWidth(lin);
  nav.beginPath();
  const grd = nav.createLinearGradient(o * 2, 0, 0, o * 2);
  nav.setLineCap('round')
  grd.addColorStop(0, '#BD1241');
  grd.addColorStop(1, '#771650');
  nav.setStrokeStyle(grd);
  nav.arc(o, o, o - lin, Math.PI * 1.5, Math.PI * cao, false);
  nav.stroke();


  nav.beginPath();
  nav.setStrokeStyle('rgba(119,22,80,0.1)');
  nav.arc(o,o,o-lin,0,Math.PI*2,false);
  nav.closePath();
  nav.stroke();

  return new Promise(function(resolve,reject){
    nav.draw(false,function(){
      wx.canvasToTempFilePath({
        canvasId: canvasid,
        success:function(res){
          resolve(res.tempFilePath)
        }
      }, this)
    })
  })
  
}
module.exports={
  geek,
  radar,
  rank
}